package by.msokoloff.javaweb2015.services.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.springframework.stereotype.Service;

import by.msokoloff.javaweb2015.dataaccess.FlightDao;
import by.msokoloff.javaweb2015.datamodel.Airport;
import by.msokoloff.javaweb2015.datamodel.Flight;
import by.msokoloff.javaweb2015.services.FlightService;

@Service
public class FlightServiceImpl implements FlightService {

	@Inject
	private FlightDao dao;

	@Override
	public Flight get(Integer id) {
		return dao.getById(id);
	}

	@Override
	public List<Flight> getDirectFlights(Airport depAirport, Airport dstAirport, Date date) {
		return dao.getDirectFlights(depAirport, dstAirport, date);
	}

	@Override
	public List<Flight> getDirectFlights(Airport depAirport, Airport dstAirport, Date dateFrom, Date dateTo) {
		return dao.getDirectFlights(depAirport, dstAirport, dateFrom, dateTo);
	}

	@Override
	public List<List<Flight>> getInDirectFlights(Airport depAirport, Airport dstAirport, Date date) {
		return dao.getInDirectFlights(depAirport, dstAirport, date);
	}

	@Override
	public List<List<Flight>> getInDirectFlights(Airport depAirport, Airport dstAirport, Date dateFrom, Date dateTo) {
		return dao.getInDirectFlights(depAirport, dstAirport, dateFrom, dateTo);
	}

	@Override
	public List<Flight> getAll() {
		return dao.getAll();
	}

	@Override
	public void createOrUpdate(Flight flight) {
		if (flight.getId() == null) {
			dao.create(flight);
		} else {
			dao.update(flight);
		}
	}

	@Override
	public void delete(Flight flight) {
		dao.delete(flight.getId());
	}

	@Override
	public void deleteAll() {
		dao.deleteAll();
	}

	@Override
	public long getCount() {
		return dao.getCount();
	}

	@Override
	public List<Flight> getAll(SingularAttribute<Flight, ?> sortParam, boolean ascending, int first, int count) {
		return dao.getAll(sortParam, ascending, first, count);
	}

	@Override
	public List<Flight> getByField(SingularAttribute<Flight, ?> attr, Object val) {
		return dao.getByField(attr, val);
	}

	@Override
	public void deleteWithRelations(Flight flight) {
		// TODO later
		delete(flight);
	}

}
